package com.android.BluethootChess.connectivity;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import android.graphics.Point;

import com.android.BluethootChess.util.Utils;

/**
 * ServThread class with features to receive, send a message and others.
 */
// public class WirelessServer extends Thread implements IComunication{
public class WirelessServer implements IComunication {

	private Socket socket;
	private DataOutputStream send;
	private DataInputStream recv;

	public WirelessServer() {
		socket = null;
		try {
			ServerSocket serverSock = new ServerSocket(Utils.PORT);
			socket = serverSock.accept();

			send = new DataOutputStream(socket.getOutputStream());
			recv = new DataInputStream(socket.getInputStream());

		} catch (Throwable e) {
			Utils.logFile("Advance gameErr: " + e);
		}

		Utils.logFile("Adresa IP: " + socket.getInetAddress().getHostAddress());

	}

	/**
	 * Returns two points representing the move(current and next position).
	 * 
	 * @return The current and the next position
	 */
	public Point[] receivePoints() {
		Point points[] = null;
		int[] coords = null;

		try {
			int lenght = recv.readInt();
			points = new Point[lenght];
			coords = new int[lenght * 2];

			for (int i = 0; i < coords.length; i++) {
				coords[i] = recv.readInt();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		points[0] = new Point(coords[0], coords[1]);
		points[1] = new Point(coords[2], coords[3]);

		return points;
	}

	/**
	 * Sends two points representing the previous and the next position.
	 * 
	 * @param points
	 *            The points that are sent
	 */
	public void sendPoints(Point[] points) {
		try {
			send.writeInt(points.length);
			for (int i = 0; i < points.length; i++) {
				send.writeInt(points[i].x);
				send.writeInt(points[i].y);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void sendString(String string) {
		try {
			Utils.logFile("Se trimite: " + string);
			Utils.logFile("Send_value = " + send);

			send.writeUTF(string.toString());
		} catch (Throwable e) {
			Utils.logFile("SendString error: " + e.toString());
			Utils.logFileError(e);
		}
	}

	public String recvString() {
		String result = "";
		try {
			result = recv.readUTF();
		} catch (Throwable e) {
			Utils.logFile("RecvString error: " + e.toString());
			Utils.logFileError(e);
		}

		return result;
	}

	public void sendPointsAsString(Point[] points) {
		String pointsAsString = "";
		for (Point p : points) {
			pointsAsString += p.x + ":" + p.y + ";";
		}
		sendString(pointsAsString);
	}

	public Point[] getPointsFromServer() {
		Point[] points = new Point[2];
		String string = recvString();
		String[] split = string.split(";");
		int i = 0;
		for (String s : split) {
			String[] coords = s.split(":");
			int x = Integer.parseInt(coords[0]);
			int y = Integer.parseInt(coords[1]);
			points[i++] = new Point(x, y);
		}

		return points;
	}

}
